package com.dinree.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.base.library.BaseFragment;
import com.base.library.manager.GlideImageManager;
import com.dinree.R;
import com.dinree.activity.AuthenticationActivity;
import com.dinree.activity.CouponWebActivity;
import com.dinree.activity.MyOrderWebActivity;
import com.dinree.activity.SettingActivity;
import com.dinree.activity.UserInfoActivity;
import com.dinree.api.UserApi;
import com.dinree.bean.UserBean;
import com.dinree.databinding.FragmentMineBinding;
import com.dinree.utils.Constant;
import com.dinree.utils.LaunchUtil;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import static com.base.library.Constant.WINDOW_DURATION;

/**
 * @author 谢铖志 on 2017/12/8 16:01
 * @version V1.0
 * @modificationHistory
 * @modify by user: 谢铖志 on 2017/12/8 16:01
 */

public class MineFragment extends BaseFragment {
    private FragmentMineBinding fragmentMineBinding;
    private UserBean userBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initData(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        fragmentMineBinding = getViewDataBinding();
        userBean = realm.findFirst(UserBean.class);
        RxView.clicks(fragmentMineBinding.userInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, UserInfoActivity.class);
            startActivity(intent);
        });
        RxView.clicks(fragmentMineBinding.dfk).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selforders?q=pay");
        });
        RxView.clicks(fragmentMineBinding.dsh).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selforders?q=sign");
        });
        RxView.clicks(fragmentMineBinding.dpj).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selforders?q=comment");
        });
        RxView.clicks(fragmentMineBinding.thh).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selforders?q=return");
        });
        RxView.clicks(fragmentMineBinding.mySc).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/like");
        });
        RxView.clicks(fragmentMineBinding.address).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/consignee?from=self");
        });
        RxView.clicks(fragmentMineBinding.myZx).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/consultation");
        });
        RxView.clicks(fragmentMineBinding.myYhq).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, CouponWebActivity.class);
            intent.putExtra("url", Constant.WEB_HOST + "front/#/selfcoupon");
            startActivity(intent);
        });
        RxView.clicks(fragmentMineBinding.myComm).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/selfcomment");
        });
        RxView.clicks(fragmentMineBinding.myShare).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/share?platform=android");
        });
        RxView.clicks(fragmentMineBinding.all).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, MyOrderWebActivity.class);
            intent.putExtra("url", Constant.WEB_HOST + "front/#/selforders");
            startActivity(intent);
        });
        RxView.clicks(fragmentMineBinding.lxkf).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, "http://120.78.199.241/epsea/front/#/serviceindex");
        });
        RxView.clicks(fragmentMineBinding.setting).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, SettingActivity.class);
            startActivity(intent);
        });
        loadData();
    }

    public void loadData() {
        if (userBean != null) {
            UserApi.getInstance(mContext).wait_comment(userBean.user_id, userBean.user_token).subscribe(s -> {
                fragmentMineBinding.dfk.setTextUnread(s.pay);
                fragmentMineBinding.dsh.setTextUnread(s.sign);
                fragmentMineBinding.dpj.setTextUnread(s.comment);
                fragmentMineBinding.thh.setTextUnread(s.refund);
            }, e -> {
            });
            UserApi.getInstance(mContext).get_dealer_state(userBean.user_id, userBean.user_token).subscribe(s -> {
                if (s != null) {
                    switch (s.state) {
                        case 0:
                            RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
                                Intent intent = new Intent(mContext, AuthenticationActivity.class);
                                startActivity(intent);
                            });
                            fragmentMineBinding.authInfo.setText("认证经销商");
                            break;
                        case 1:
                            RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {

                            });
                            fragmentMineBinding.authInfo.setText("审核中");
                            break;
                        case 2:
                            RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {

                            });
                            fragmentMineBinding.authInfo.setText("经销商");
                            break;
                        case 3:
                            RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
                                Intent intent = new Intent(mContext, AuthenticationActivity.class);
                                startActivity(intent);
                            });
                            fragmentMineBinding.authInfo.setText("认证经销商");
                            break;
                    }
                }
            }, e -> {
            });
            UserApi.getInstance(mContext).get_basic_info(userBean.user_id, userBean.user_token).subscribe(s -> {
                fragmentMineBinding.nickname.setText(s.nickname);
                fragmentMineBinding.number.setText("编号：" + s.user_no);
//                if (s.user_type == 1) {
//                    RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
//                        Intent intent = new Intent(mContext, CompanyAuthActivity.class);
//                        startActivity(intent);
//                    });
//                    fragmentMineBinding.authInfo.setText("认证经销商");
//                } else {
//                    RxView.clicks(fragmentMineBinding.authInfo).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
//
//                    });
//                    fragmentMineBinding.authInfo.setText("经销商");
//                }
                GlideImageManager.getInstance(mContext).displayImagePlaceAvatar(s.avatar, fragmentMineBinding.imgHead);
            }, e -> {
            });
        }
    }
}
